﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;
using BestBuy.Common;

namespace BestBuy.PageGuest
{
    public partial class bBuyOfferDetail : System.Web.UI.Page
    {
        CProduct _csp = new CProduct();
        CBuyerOffer _cb = new CBuyerOffer();
        CSellerOffer _cs = new CSellerOffer();
        CDanhMuc _cdm = new CDanhMuc();
        CMessage _csms = new CMessage();
        BangTinMua sp = new BangTinMua();
        protected void Page_Load(object sender, EventArgs e)
        {
            pnError.Visible = false;
            pnContact.Visible = false;
            LoadInFo();
        }

        private void CheckMessage()
        {
            if (!Session["TenDN"].ToString().Equals(""))
            {
                int IDTKSender = Convert.ToInt32(Session["IDDN"].ToString());
                int IDTKReceiver = sp.IDTaiKhoan;
                BMessage sms = _csms.GetMessageExists(IDTKSender, IDTKReceiver, IDTKReceiver,(int)SourceContactStatus.Account);
                if (sms != null)
                {
                    pnContact.Visible = true;
                    lbNoiDung.Text = sms.Subject;
                }
            }
        }

        private string BuyingFrequency(int soluong) {
            string str = "";
            if (soluong == 1)
            {
                str = "Daily";
            }
            else if (soluong == 2)
            {
                str = "One time";
            }
            else if (soluong == 3)
            {
                str = "Monthly";
            }
            else if (soluong == 4)
            {
                str = "Yearly";
            }
            return str;
        }

        private string PaymentType(string phuongthuc) {
            String[] strmang = phuongthuc.Split('+');
            string str = "";
            if (strmang[0] == "1")
            {
                str += "Paypal, ";
            }
            if (strmang[1] == "2")
            {
                str += "Western Union, ";
            }
            if (strmang[2] == "3")
            {
                str += "Money Gram, ";
            }
            if (strmang[3] == "4")
            {
                str += "T/T, ";
            }
            if (strmang[4] == "5")
            {
                str += "D/P, ";
            }
            if (strmang[5] == "6")
            {
                str += "L/C, ";
            }
            if (strmang[6] == "7")
            {
                str += "Other, ";
            }
            return str;
        }

        private void LoadInFo()
        {
            if (Request.Params["pid"] != null)
            {
                try
                {
                    int id = Convert.ToInt32(Request.Params["pid"].ToString());
                    //load thong tin sp
                    sp = _cb.GetBuyerOfferID(id);
                    lbName.Text = sp.TieuDe;
                    lbCountry.Text = sp.ThongTinTaiKhoan.QuocGia.TenQuocGia;
                    if (sp.DonVi != null)
                    {
                        lbQuantityRequired.Text = sp.SoLuongMua.ToString() + "/" + sp.DonVi.TenDonVi;
                        lbAnnual.Text = sp.KhoiLuongMuaHangNam.ToString() + "/" + sp.DonVi.TenDonVi;
                    }
                    lbCreate.Text = sp.NgayDang;
                    lbExpired.Text = sp.ThoiHanDang;
                    if (sp.TanSuatMuaHang != null)
                    {
                        lbFrequency.Text = BuyingFrequency(sp.TanSuatMuaHang.Value);
                    }else{
                        lbFrequency.Text = "";
                    }
                    lbPayment.Text = PaymentType(sp.PhuongThucThanhToan);
                    lbDescription.Text = sp.NoiDung;
                    lbMota.Text = sp.MoTa;
                    imgCountry.ImageUrl = "~/Images/Flags/" + sp.ThongTinTaiKhoan.QuocGia.TenQuocGia + ".gif";
                    if (sp.ThongTinTaiKhoan.LoaiND.Value == 1)
                    {
                        lbh4Contact.Text = " Buyer";
                    }
                    else if (sp.ThongTinTaiKhoan.LoaiND.Value == 2)
                    {
                        lbh4Contact.Text = " Seller";
                    }
                    lbPerson.Text = sp.ThongTinTaiKhoan.HoTen;
                    lbCompanyContact.Text = "< " + sp.ThongTinTaiKhoan.CongTy.TenCongTy + " >";
                    //load menu quick
                    hlProduct.NavigateUrl = "~/PageGuest/PageProductOfAccount.aspx?aid=" + sp.IDTaiKhoan;
                    hlBuy.NavigateUrl = "~/PageGuest/PageBuyOfAccount.aspx?aid=" + sp.IDTaiKhoan;
                    hlSell.NavigateUrl = "~/PageGuest/PageSellOfAccount.aspx?aid=" + sp.IDTaiKhoan;

                    //load linkQuick
                    DanhMuc danhmuc = _cb.GetCatalogyOfIDBangTinMua(sp.IDBangTinMua);
                    HyperLink hl = new HyperLink();
                    hl.ID = "hl" + id.ToString();
                    hl.NavigateUrl = "~/PageGuest/bListOfCategoryLevel1.aspx?pid="+danhmuc.IDDanhMuc;
                    hl.Text = danhmuc.TenDanhMuc;
                    Label lb = new Label();
                    lb.ID = "lb" + id.ToString();
                    lb.Text = " > " + sp.TieuDe;
                    linkQuick.Controls.Add(hl);
                    linkQuick.Controls.Add(lb);

                    //load count product,sell,buy
                    lbCountProduct.Text = _csp.GetCountProductOfID(sp.IDTaiKhoan).ToString();
                    lbCountBuy.Text = _cb.GetCountBuyOfID(sp.IDTaiKhoan).ToString();
                    lbCountSell.Text = _cs.GetCountSellOfID(sp.IDTaiKhoan).ToString();

                    //load relate product
                    List<BangTinMua> listbangtin = _cb.GetListOfIDCatalogyRelate(sp.IDBangTinMua, 0, 20);
                    if (listbangtin.Count > 0)
                    {
                        dlRelatedProducts.DataSource = listbangtin;
                        dlRelatedProducts.DataBind();
                    }

                    CheckMessage();

                }
                catch
                {
                    Response.Redirect("~/PageGuest/PageError.aspx");
                }
            }
            else
            {
                Response.Redirect("~/PageGuest/PageError.aspx");
            }
        }

        protected void btnContact_Click(object sender, EventArgs e)
        {
            Response.Redirect("PageContactMessage.aspx?ml=2&pid=" + sp.IDBangTinMua.ToString());
        }

        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            if (Session["TenDN"].ToString().Equals(""))
            {
                Response.Redirect("~/Account/Login.aspx");
                return;
            }
            if (txtMessage.Text.Equals(""))
            {
                pnError.Visible = true;
                pnError.CssClass = "InLeft error message";
                lbError.Text = "Message is not emtry.";
                return;
            }
            BMessage sms = new BMessage();
            sms.IDTKSender = Convert.ToInt32(Session["IDDN"].ToString());
            sms.IDTKReceiver = sp.IDTaiKhoan;
            if (sms.IDTKSender == sms.IDTKReceiver)
            {
                pnError.Visible = true;
                pnError.CssClass = "InLeft error message";
                lbError.Text = "You can't contact this account.";
                return;
            }
            sms.Subject = txtMessage.Text;
            sms.Status = (int)MessageStatus.SentUnread;
            sms.Source = (int)SourceContactStatus.Account;
            sms.Date = DateTime.Now.ToString("MM/dd/yyyy");
            sms.IDContact = sp.IDTaiKhoan;
            bool kq = _csms.InsertMessage(sms);
            if (kq)
            {
                pnError.Visible = true;
                pnError.CssClass = "InLeft success message";
                lbError.Text = "Send message successfully.";
                txtMessage.Text = "";
            }
        }
    }
}